/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package koncowka;

import com.opencsv.CSVReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author root
 */
public class Skaner extends Thread {

    private boolean czyPierwsze;
    private Process proc;
    private Runtime rt;
    private Boolean stop;
    private static long SLEEPTIME = 30000;

    public Skaner() {
        try {
            stop = false;
            String skrypt[];
            ProcessBuilder pb = new ProcessBuilder("src/skrypty/pierwszy.sh");
            pb.redirectError(new File("zapis-01.csv"));
            proc = pb.start();
        } catch (IOException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void koniec() {
        stop = true;
        proc.destroy();
        try {
            String skrypt[];
            skrypt = new String[]{"bash", "src/skrypty/./ostatni.sh"};
            proc = Runtime.getRuntime().exec(skrypt);
            // proc.getOutputStream().close();
            proc.getInputStream().close();
            proc.getErrorStream().close();
            // System.setOut(new PrintStream(proc.getOutputStream()));
        } catch (IOException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public synchronized void run() {
        super.run();
        try {
            this.wait(1000);
            while (!stop) {
               // proc.getOutputStream().flush();
                File file = new File("zapis-01.csv");
                //System.out.println(file.getAbsolutePath());
                CSVReader reader = new CSVReader(new FileReader(file), ',');
                //String[] nextLine;
                Boolean znacznik = false;
                for (String[] nextLine : reader.readAll()) {
                    if (nextLine[0].isEmpty()) {
                        //System.out.println("Brak ciagu danych");
                        continue;
                    }
                    if (nextLine[0].equals("BSSID")) {
                        znacznik = false;
                        continue;
                    }
                    if (nextLine[0].equals("Station MAC")) {
                        znacznik = true;
                        continue;
                    }
                    if (znacznik) {
                        skanujKlient(nextLine);
                    } else {
                        skanujAp(nextLine);
                    }
                }
                this.wait(SLEEPTIME);
                //Thread.sleep(SLEEPTIME);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void skanujKlient(String[] nextLine) {
        try {
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Klient kl = new Klient(nextLine[0].trim(), formater.parse(nextLine[1].trim()), formater.parse(nextLine[2].trim()), Integer.parseInt(nextLine[3].trim()), nextLine[5].trim());

            for (Ap ap : Koncowka.getListaAp()) {
                if (ap.getBSSID().equals(kl.getApBSSID())) {
                    if (!ap.getListaKlientow().contains(kl)) {
                        ap.getListaKlientow().add(kl);
                        break;
                    } else {
                        for (Klient klient : ap.getListaKlientow()) {
                            if (kl.equals(klient)) {
                                klient.update(kl);
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        } catch (ParseException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void skanujAp(String[] nextLine) {
        try {
            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Ap ap = new Ap(nextLine[0].trim(), formater.parse(nextLine[1].trim()), formater.parse(nextLine[2].trim()), Integer.parseInt(nextLine[3].trim()), Integer.parseInt(nextLine[4].trim()), Integer.parseInt(nextLine[8].trim()), nextLine[13].trim(), nextLine[14].trim());
            if (Koncowka.getListaAp().contains(ap)) {
                for (Ap listaAp : Koncowka.getListaAp()) {
                    if (ap.equals(listaAp)) {
                        listaAp.update(ap);
                        break;
                    }
                }
            } else {
                Koncowka.getListaAp().add(ap);
            }
        } catch (ParseException ex) {
            Logger.getLogger(Skaner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
